package com.secneo.appshare.threads;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.RemoteApi.Feeds;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;

public class PhoneListThread extends Thread 
{
	private final static String TAG = "MyTag";
	private Handler handler;
	private Context ctx;
	private int number;
	private List<String> phoneList;
	
	private boolean running = true;
	
	public PhoneListThread(Context ctx, Handler handler,int number) 
	{
		this.handler = handler;
		this.ctx = ctx;
		this.number = number;
	}
	
	public synchronized void stopRun()
	{
		running = false;
		this.interrupt();
	}
	
	public List<String> getPhoneList() {
		return phoneList;
	}
	
	public void run() 
	{
		try 
		{			
			running = true;
			RemoteApiImpl remoteApi = new RemoteApiImpl();
			phoneList = remoteApi.getTopPhones(ctx, number);
			
			if(null==phoneList){
				Message msg = new Message();
				msg.what = Constants.MSG_GET_TOP_PHONE_NETWORK_ERROR;
				handler.sendMessage(msg);
				return;
			}
			
			Message msg = new Message();
			msg.what = Constants.MSG_GET_TOP_PHONE_LIST_FINISH;
			handler.sendMessage(msg);
			
		} catch (Exception ex) {
			Log.d(TAG, "GET_TOP_PHONE::Run() error = "+ex.getMessage());
			Message msg = new Message();
			msg.what = Constants.MSG_GET_TOP_PHONE_NETWORK_ERROR;
			handler.sendMessage(msg);
		}
	}
}
